@using BTCPayServer.Services.PaymentRequests
@using System.Globalization
@using BTCPayServer.Client
@using BTCPayServer.Controllers
@using BTCPayServer.Forms
@using BTCPayServer.Plugins.Emails
@using BTCPayServer.TagHelpers
@using Microsoft.AspNetCore.Mvc.TagHelpers
@inject FormDataService FormDataService
@inject LinkGenerator LinkGenerator
@model BTCPayServer.Models.PaymentRequestViewModels.UpdatePaymentRequestViewModel
@{
    var checkoutFormOptions = await FormDataService.GetSelect(Model.StoreId, Model.FormId);
    ViewData.SetLayoutModel(new LayoutModel("PaymentRequests", string.IsNullOrEmpty(Model.Id) ? StringLocalizer["Create Payment Request"] : StringLocalizer["Edit Payment Request"]));
}

@section PageHeadContent {
    <link href="~/vendor/summernote/summernote-bs5.css" rel="stylesheet" asp-append-version="true" />
}

@section PageFootContent {
    <partial name="_ValidationScriptsPartial" />
    <script src="~/vendor/summernote/summernote-bs5.js" asp-append-version="true"></script>
}

<form method="post" action="@Url.Action("EditPaymentRequest", "UIPaymentRequest", new { storeId = Model.StoreId, payReqId = Model.Id }, Context.Request.Scheme)">
    <div class="sticky-header">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a asp-action="GetPaymentRequests" asp-route-storeId="@Model.StoreId" text-translate="true">Requests</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">@ViewData["Title"]</li>
            </ol>
            <h2>@ViewData["Title"]</h2>
        </nav>
        <div>
            @if (string.IsNullOrEmpty(Model.Id))
            {
                <button id="page-primary" type="submit" class="btn btn-primary" permission="@Policies.CanModifyPaymentRequests" text-translate="true">Create</button>
            }
            else
            {
				<button id="page-primary" type="submit" class="btn btn-primary order-sm-1" permission="@Policies.CanModifyPaymentRequests" text-translate="true">Save</button>
                <a class="btn btn-secondary" asp-action="ViewPaymentRequest" asp-route-payReqId="@Model.Id" id="ViewPaymentRequest" target="_blank" text-translate="true">View</a>
            }
        </div>
    </div>

    <partial name="_StatusMessage" />

    <div class="row">
        <div class="col-xl-8 col-xxl-constrain">
            @if (!ViewContext.ModelState.IsValid)
            {
                <div asp-validation-summary="ModelOnly"></div>
            }
            <div class="form-group">
                <label asp-for="Title" class="form-label" data-required></label>
                <input asp-for="Title" class="form-control" required />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="d-flex justify-content-between">
                <div class="form-group flex-fill me-4">
                    <label asp-for="Amount" class="form-label" data-required></label>
                    <input type="number" inputmode="decimal" step="any" asp-for="Amount" class="form-control" required disabled="@(!Model.AmountAndCurrencyEditable)" />
                    <span asp-validation-for="Amount" class="text-danger"></span>
                    @if (!Model.AmountAndCurrencyEditable)
                    {
                        <p class="info-note text-warning mb-0 mt-2">
                            <vc:icon symbol="warning"/>
                            <span text-translate="true">Amount and currency are not editable once payment request has invoices</span>
                        </p>
                    }
                </div>
                <div class="form-group">
                    <label asp-for="Currency" class="form-label"></label>
                    <input asp-for="Currency" class="form-control w-auto" currency-selection disabled="@(!Model.AmountAndCurrencyEditable)" />
                    <span asp-validation-for="Currency" class="text-danger"></span>
                </div>
            </div>
            <div class="form-group d-flex">
                <input asp-for="AllowCustomPaymentAmounts" type="checkbox" class="btcpay-toggle me-3" />
                <label asp-for="AllowCustomPaymentAmounts" class="form-check-label"></label>
                <span asp-validation-for="AllowCustomPaymentAmounts" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ReferenceId" class="form-label"></label>
                <input asp-for="ReferenceId" class="form-control" />
                <span asp-validation-for="ReferenceId" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ExpiryDate" class="form-label"></label>
                <div class="input-group ">
                    <input asp-for="ExpiryDate"
                           value="@(Model.ExpiryDate?.ToString("u", CultureInfo.InvariantCulture))"
                           class="form-control flatdtpicker" min="today" placeholder="@StringLocalizer["No expiry date has been set for this payment request"]" />
                    <button id="ClearExpiryDate" class="btn btn-secondary input-group-clear" type="button" title="@StringLocalizer["Clear"]">
                        <vc:icon symbol="close" />
                    </button>
                </div>
                <span asp-validation-for="ExpiryDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Email" class="form-label"></label>
                <input type="email" asp-for="Email" placeholder="@StringLocalizer["Firstname Lastname <email@example.com>"]" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span>
                <div id="PaymentRequestEmailHelpBlock" class="form-text">
                    @ViewLocalizer["This will send notification mails to the recipient, as configured by the <a href=\"{0}\">email rules</a>.",
                         LinkGenerator.GetStoreEmailRulesLink(Model.StoreId, Context.Request.GetRequestBaseUrl())]
                    @if (Model.HasEmailRules is not true)
                    {
                        <div class="info-note mt-1 text-warning" role="alert">
                            <vc:icon symbol="warning"/>
                            <span text-translate="true">No payment request related email rules have been configured for this store.</span>
                        </div>
                    }
                </div>
            </div>
            <div class="form-group">
                <label asp-for="FormId" class="form-label"></label>
                <select asp-for="FormId" class="form-select w-auto" asp-items="@checkoutFormOptions"></select>
                <span asp-validation-for="FormId" class="text-danger"></span>
            </div>

            @if (Model.FormResponse is not null)
            {
                <div class="bg-tile rounded py-2 px-3 mb-5">
                    <partial name="PosData" model="(Model.FormResponse, 1)"/>
                </div>
            }
        </div>
    </div>

    <div class="row">
        <div class="col-xl-10 col-xxl-constrain">
            <div class="form-group">
                <label asp-for="Description" class="form-label"></label>
                <textarea asp-for="Description" class="form-control richtext"></textarea>
                <span asp-validation-for="Description" class="text-danger"></span>
            </div>
        </div>
    </div>
</form>

@if (!string.IsNullOrEmpty(Model.Id))
{
    <div class="d-flex gap-3 mt-3">
        <a class="btn btn-secondary"
            permission="@Policies.CanViewInvoices"
           asp-action="ListInvoices"
           asp-controller="UIInvoice"
           asp-route-storeId="@Model.StoreId"
           asp-route-searchterm="@($"orderid:{PaymentRequestRepository.GetOrderIdForPaymentRequest(Model.Id)}")" text-translate="true">Invoices</a>
        <a class="btn btn-secondary" asp-route-payReqId="@Model.Id" asp-action="ClonePaymentRequest" id="ClonePaymentRequest"  permission="@Policies.CanModifyPaymentRequests" text-translate="true">Clone</a>
        @if (!Model.Archived)
        {
            <a class="btn btn-secondary" data-bs-toggle="tooltip" title="@StringLocalizer["Archive this payment request so that it does not appear in the payment request list by default"]" asp-controller="UIPaymentRequest" asp-action="TogglePaymentRequestArchival" asp-route-payReqId="@Model.Id" id="ArchivePaymentRequest" permission="@Policies.CanModifyPaymentRequests" text-translate="true">Archive</a>
        }
        else
        {
            <a class="btn btn-secondary" data-bs-toggle="tooltip" title="@StringLocalizer["Unarchive this payment request"]" asp-controller="UIPaymentRequest" asp-action="TogglePaymentRequestArchival" asp-route-payReqId="@Model.Id" id="UnarchivePaymentRequest" permission="@Policies.CanModifyPaymentRequests" text-translate="true">Unarchive</a>
        }
    </div>
}
